package net.lzzy.practices.services;

import net.lzzy.practices.entities.User;
import net.lzzy.practices.repositories.UserProjection;
import net.lzzy.practices.repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
 * @author 1
 */
@Service
public class UserServiceImpl implements UserService {

    private final UserRepository repository;

    @Autowired
    public UserServiceImpl(UserRepository repository) {
        this.repository = repository;
    }

    /**
     * 登陆时检查用户名和密码是否正确
     *
     * @param username
     * @param password
     * @return 检查正确则返回user否则围殴null
     */
    @Override
    public User check(String username, String password) {
        return repository.findUserByUserNameAndPassword(username,password);
    }

    /**
     * 获取salt
     *
     * @param username
     * @return
     */
    @Override
    public String getSalt(String username) {
        List<UserProjection> users = repository.getUsersByUserName(username);
        if (users == null || users.size()== 0) {
            return "";
        }
        UserProjection user = users.get(0);
        String salt = user.getSalt();
        return salt == null ? "" : salt;
    }

    /**
     * 注册用户
     *
     * @param user
     * @return 成功返回user
     */
    @Override
    public User register(User user) {
        return repository.save(user);
    }

    /**
     * 判断用户是否存在
     *
     * @param username
     * @return 布尔值，是否占用
     */
    @Override
    public boolean isUsernameOccupied(String username) {
        return repository.countByUserName(username)>0;
    }


    @Override
    public List<User> get(){
        return repository.findAllByOrderByUpdateTimeDesc();
    }

    /**
     * 分页获取所有用户
     * @param page 当前页码
     * @param size 每页数据大小
     * @return
     */
    @Override
    public List<User> get(int page,int size){
        return repository.findUserByApplyTeacherOrderByUpdateTimeDesc(PageRequest.of(page,size));
    }
//PageRequest.of(page,size)
    /**
     * 统计用户数量
     * @return 用户数量
     */
    @Override
    public Integer count(){
        return repository.countAllBy();
    }

    /**
     *
     * @param kw
     * @return
     */
    @Override
    public List<User> search(String kw){
        return repository.findByQuery(kw);
    }

    /**
     * 获取申请教师资格的用户
     *
     * @param applying true表示申请教师用户
     * @return
     */
    @Override
    public List<User> getByApplyTeacher(boolean applying) {
        return Collections.emptyList();
    }

    /**
     * 获取申请教师资格的用户
     *
     * @param applying true表示申请教师用户
     * @return
     */
//    @Override
//    public List<User> getByApplyTeacher(boolean applying) {
//        return Collections.emptyList();
//    }

    /**
     * 获取申请教师资格的用户
     * @param applying true表示申请教师用户
     * @return
     */
    public List<User> getByApplyTeacher(PageRequest applying){
        return repository.findUserByApplyTeacherOrderByUpdateTimeDesc(applying);
    }
    //findUserByApplyTeacherOrderByUpdateTimeDesc(applying)

    /**
     * 审批通过教师资格
     * @param id
     * @return
     */

    @Override
     public User applyTeacher(UUID id){
        return null;
    }

    /**
     *  审核不通过
     * @param id
     * @return
     */
    @Override
    public User declineTeacher(UUID id){
        User user = repository.findById(id).orElse(null);
        if(user != null){
            user.setApplyTeacher(false);
            return repository.save(user);
        }
          return null;
    }

    /**
     *  禁止该用户登录
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
     public User banUser(UUID id){
        User user = repository.findById(id).orElse(null);
        if(user != null && user.getType() != User.TYPE_ADMIN){
            user.setType(User.TYPE_BANNED);
            return repository.save(user);
        }
        return null;
    }

    /**
     * @param id 用户ID
     * @return 用户对象
     */
    @Override
    public User getById(UUID id) {
        return repository.findById(id).orElse(null);
    }
}
